﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class GetUserCommandHander : IRequestHandler<GetUserCommand, Apipage<UserDto>>
    {
        public IRepistory<User> _Usersrepistory;
        public IRepistory<Role>_Rolesrepistory;
        public IRepistory<UserRole> userrolerepistory;
        public GetUserCommandHander(IRepistory<User> Usersrepistory, IRepistory<Role> Rolesrepistory, IRepistory<UserRole> userrolerepistory)
        {
                _Usersrepistory = Usersrepistory;
                _Rolesrepistory = Rolesrepistory;
                this.userrolerepistory = userrolerepistory;
        }
        
        public Task<Apipage<UserDto>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            Apipage<UserDto> result=new Apipage<UserDto>();
            var linq = _Usersrepistory.GetAll().Where(x => !x.IsDeleted);
            var list = from a in linq
                       join b in userrolerepistory.GetAll() on a.UserId equals b.UserId
                       join c in _Rolesrepistory.GetAll() on b.RoleId equals c.RoleId
                       select new UserDto
                       {
                           UserId = a.UserId,
                           UserName = a.UserName,
                           UserSex = a.UserSex,
                           Province = a.Province,
                           City = a.City,
                           County = a.County,
                           UserAddress = a.UserAddress,
                           Updatedate = a.Updatedate,
                           Updateor = a.Updateor,
                           CreationDate = a.CreationDate,
                           Creator = a.Creator,
                           RoleName = c.RoleName,
                           RoleId = c.RoleId,
                       };
            result.code = Renum.显示成功;
            result.Message=Renum.显示成功.ToString();
            result.TotalCount = list.Count();
            result.PageCount=(int)Math.Ceiling(result.TotalCount*1.0/request.PageSize);
            var lista = list.ToList();
            var s = lista.GroupBy(q => q.UserId);
            var t = new List<UserDto>();
            foreach (var item in s)
            {
                string str = "";
                foreach(var item2 in item)
                {
                    if (str == "")
                    {
                        str += item2.RoleName;
                    }
                    else
                    {
                        str += "," + item2.RoleName;
                    }
                }
                item.FirstOrDefault().RoleNames = str;
            }
            foreach(var item in s)
            {
                t.Add(item.FirstOrDefault());
            }
            result.Items=t.Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
    }
}
